package com.shenmazong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shenmazong.entity.TbVideo;
import com.shenmazong.mapper.TbVideoMapper;
import com.shenmazong.service.TbVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.ListParamVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;

/**
 * <p>
 * 教学视频 ——>  John 服务实现类
 * </p>
 *
 * @author 还怕大雨吗
 * @since 2021-09-18
 */
@Service
public class TbVideoServiceImpl implements TbVideoService {

    @Resource
    private TbVideoMapper tbVideoMapper;
    /**
     * @description 查询视频
     * @params [listParamVo]
     * @return com.shenmazong.utils.ResultResponse
     * @author -还怕大雨吗
     * @date 2021/9/21 19:40
     */
    @Override
    @Cacheable(value = "selectVideo")
    public ResultResponse selectVideo(ListParamVo listParamVo) {
        IPage<TbVideo> page = tbVideoMapper.selectVideoAll(
                new Page<TbVideo>(listParamVo.getCurrent(),listParamVo.getSize()),
                listParamVo.getParamMap().get("title")
        );
        return ResultResponse.SUCCESS(page);
    }

    /***
     * @description 新增视频
     * @params [tbVideo]
     * @return com.shenmazong.utils.ResultResponse
     * @author -还怕大雨吗
     * @date 2021/9/21 19:41
     */
    @Override
    @CacheEvict(value = "selectVideo",allEntries = true)
    public ResultResponse insertVideo(TbVideo tbVideo) {
        TbVideo tsb = tbVideoMapper.selectOne(
                new QueryWrapper<TbVideo>().lambda().eq(TbVideo::getVideoTitle, tbVideo.getVideoTitle()).last(
                        "limit 1"
                )
        );
        if (tsb != null){
            return ResultResponse.FAILED("标题已存在，请重试");
        }
        tbVideoMapper.insert(tbVideo);
        return ResultResponse.SUCCESS();
    }
    /***
     * @description 修改视频信息，
     * @params [tbVideo]
     * @return com.shenmazong.utils.ResultResponse
     * @author -还怕大雨吗
     * @date 2021/9/21 19:44
     */
    @Override
    @CacheEvict(value = "selectVideo",allEntries = true)
    public ResultResponse updateVideo(TbVideo tbVideo) {
        int insert = tbVideoMapper.updateById(tbVideo);
        return ResultResponse.SUCCESS();
    }
    /***
     * @description 删除视频信息
     * @params [tbVidel]
     * @return com.shenmazong.utils.ResultResponse
     * @author -还怕大雨吗
     * @date 2021/9/21 19:45
     */
    @Override
    @CacheEvict(value = "selectVideo",allEntries = true)
    public ResultResponse deleteVideo(Long tbVidel) {
        int i = tbVideoMapper.deleteById(tbVidel);
        return ResultResponse.SUCCESS();
    }


}
